package com.hrrt.home.admin.controller.manage;

import com.hrrt.common.base.BaseController;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.HomeBaseDevice;
import com.hrrt.home.dao.model.HomeBaseDeviceExample;
import com.hrrt.home.rpc.api.HomeBaseDeviceService;
import com.hrrt.home.rpc.api.HomeDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础设备controller
 * Created by JiangyanPeng on 2017/09/01.
 */
@Controller
@RequestMapping("/manage/basedevice")
@Api(value = "基础设备", description = "基础设备")
public class HomeBaseDeviceController extends BaseController {
    @Autowired
    HomeBaseDeviceService homeBaseDeviceService ;
    @Autowired
    HomeDeviceTypeService homeDeviceTypeService;


    private static Logger _log = LoggerFactory.getLogger(HomeBaseUnitController.class);

    @ApiOperation(value = "基础设备首页")
    @RequiresPermissions("home:basedevice:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/basedevice/index.jsp";
    }
    @ApiOperation(value = "基础设备列表")
    @RequestMapping(value="list",method = RequestMethod.GET)
    @RequiresPermissions("home:basedevice:read")
    @ResponseBody
    public Object list (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order){
        HomeBaseDeviceExample homeBaseDeviceExample = new HomeBaseDeviceExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeBaseDeviceExample.setOrderByClause(sort + " " + order);
        }
        homeBaseDeviceExample.createCriteria().andParentIdNot(0);
        List<HomeBaseDevice> rows = homeBaseDeviceService.selectBaseDeviceByExampleForOffsetPage(homeBaseDeviceExample,offset,limit);
        long total = homeBaseDeviceService.countByExample(homeBaseDeviceExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增基础设备")
    @RequiresPermissions("home:basedevice:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        HomeBaseDeviceExample example=new HomeBaseDeviceExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<HomeBaseDevice> homeDeviceTypes = homeBaseDeviceService.selectByExample(example);
        modelMap.put("homeDeviceTypes",homeDeviceTypes);
        return "/manage/basedevice/create.jsp";
    }


    @ApiOperation(value = "新增基础设备")
    @RequiresPermissions("home:basedevice:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeBaseDevice homeBaseDevice) {
        int count = homeBaseDeviceService.insertSelective(homeBaseDevice);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改基础设备")
    @RequiresPermissions("home:basedevice:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        HomeBaseDevice homeBaseDevice = homeBaseDeviceService.selectByPrimaryKey(id);
        modelMap.put("homeBaseDevice", homeBaseDevice);
        HomeBaseDeviceExample example=new HomeBaseDeviceExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<HomeBaseDevice> homeDeviceTypes = homeBaseDeviceService.selectByExample(example);
        modelMap.put("homeDeviceTypes",homeDeviceTypes);
        return "/manage/basedevice/update.jsp";
    }

    @ApiOperation(value = "修改基础设备")
    @RequiresPermissions("home:basedevice:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeBaseDevice homeBaseDevice) {
        homeBaseDevice.setId(id);
        int count = homeBaseDeviceService.updateByPrimaryKeySelective(homeBaseDevice);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除基础设备")
    @RequiresPermissions("home:baseunit:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeBaseDeviceService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }


    @ApiOperation(value = "基础设备类型首页")
    @RequiresPermissions("home:basedevice:readType")
    @RequestMapping(value = "/typeIndex", method = RequestMethod.GET)
    public String typeIndex() {
        return "/manage/basedevice/typeIndex.jsp";
    }
    @ApiOperation(value = "基础设备类型列表")
    @RequestMapping(value="typeList",method = RequestMethod.GET)
    @RequiresPermissions("home:basedevice:readType")
    @ResponseBody
    public Object typeList (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order){
        HomeBaseDeviceExample example=new HomeBaseDeviceExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        example.createCriteria().andParentIdEqualTo(0);
        List<HomeBaseDevice> rows=homeBaseDeviceService.selectByExampleForOffsetPage(example,offset,limit);
        long total=homeBaseDeviceService.countByExample(example);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }


    @ApiOperation(value = "新增设备类型")
    @RequiresPermissions("home:basedevice:createType")
    @RequestMapping(value = "/createType",method = RequestMethod.GET)
    public String createType(){
        return "/manage/basedevice/createType.jsp";
    }

    @ApiOperation(value = "保存新增基础设备类型")
    @RequiresPermissions("home:basedevice:createType")
    @RequestMapping(value = "/createType", method = RequestMethod.POST)
    @ResponseBody
    public Object createType(HomeBaseDevice homeBaseDevice) {
        homeBaseDevice.setParentId(0);
        int count = homeBaseDeviceService.insertSelective(homeBaseDevice);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改基础设备类型")
    @RequiresPermissions("home:basedevice:updateType")
    @RequestMapping(value = "/updateType/{id}", method = RequestMethod.GET)
    public String updateType(@PathVariable("id") int id, ModelMap modelMap) {
        HomeBaseDevice homeBaseDevice=homeBaseDeviceService.selectByPrimaryKey(id);
        modelMap.put("homeBaseDevice",homeBaseDevice);
        return "/manage/basedevice/updateType.jsp";
    }

    @ApiOperation(value = "修改基础设备类型")
    @RequiresPermissions("home:basedevice:updateType")
    @RequestMapping(value = "/updateType/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object updateType(@PathVariable("id") int id, HomeBaseDevice homeBaseDevice) {
        homeBaseDevice.setId(id);
        int count = homeBaseDeviceService.updateByPrimaryKeySelective(homeBaseDevice);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除基础设备类型")
    @RequiresPermissions("home:basedevice:deleteType")
    @RequestMapping(value = "/deleteType/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteType(@PathVariable("ids") String ids) {
        int count = homeBaseDeviceService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }


}
